
<template>
  <div class="login-bg">
    <div class="register-container">
      <h2>用户注册</h2>
      <form @submit.prevent="onRegister">
        <div class="form-group">
          <label>用户名：</label>
          <input v-model="username" placeholder="请输入用户名" />
        </div>
        <div class="form-group">
          <label>邮箱：</label>
          <input v-model="email" type="text" placeholder="请输入邮箱" />
        </div>
        <div class="form-group">
          <label>密码：</label>
          <input v-model="password" type="password" placeholder="请输入密码" />
        </div>
        <button type="submit">注册</button>
        <div class="login-link" @click="router.push('/login')">已有账号？去登录</div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import router from '@/router/index.js'
import RegisterApi from '@/api/register.js'
import { useToast } from '@/components/ToastProvider.vue'

const username = ref('')
const email = ref('')
const password = ref('')

const showToast = useToast()

const onRegister = () => {
  if (!username.value) {
    showToast('用户名不能为空', true)
    return
  }
  if (!email.value) {
    showToast('邮箱不能为空', true)
    return
  }
  if (!password.value) {
    showToast('密码不能为空', true)
    return
  }

  // 发起注册请求
  RegisterApi.register({
    name: username.value,
    email: email.value,
    password: password.value
  })
    .then(res => {
      if (res.code === 0) {
        showToast('注册成功，即将跳转登录页')
        setTimeout(() => {
          router.push('/login')
        }, 1500)
      } else {
        showToast(res.msg || '注册失败', true)
      }
    })
    .catch(err => {
      console.error(err)
      showToast('注册失败，请稍后再试', true)
    })
}
</script>

<style scoped>
.login-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image:
    linear-gradient(225deg, #ffffff, #ff6ec7),
    linear-gradient(135deg, #ffffff, #007BFF);

  background-size: cover;
  background-blend-mode: soft-light;
  z-index: 0;
}


.register-container {
  position: relative;
  z-index: 1;
  width: 400px;
  padding: 40px 30px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  text-align: center;

  h2 {
    margin-bottom: 20px;
    font-size: 24px;
    color: #333;
  }

  form {
    display: flex;
    flex-direction: column;
    gap: 15px;
  }

  .form-group {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
  }

  label {
    margin-bottom: 6px;
    font-size: 14px;
    color: #555;
  }

  input {
    width: 100%;
    padding: 10px;
    font-size: 14px;
    border: 1px solid #ccc;
    border-radius: 6px;
    outline: none;
  }

  input:focus {
    border-color: #007bff;
    box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
  }

  button {
    margin-top: 10px;
    padding: 12px;
    font-size: 16px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s ease;
  }

  button:hover {
    background-color: #0056b3;
  }

  .login-link {
    margin-top: 15px;
    font-size: 14px;
    color: #007bff;
    cursor: pointer;
    &:hover {
      text-decoration: underline;
    }
  }
}

</style>
